package dk.rhok.findstufffast;



import android.location.*;
import android.graphics.*;
import android.graphics.drawable.*;
import android.widget.*;
import android.app.*;



public class ArrowManager extends ViewManager implements CompassListener
{
    private FindStuffFast findStuffFast;
    private Location phoneLocation;
    private Location targetLocation;

    

    public ArrowManager(FindStuffFast findStuffFast)
    {
        super(R.layout.arrow, 7f, Float.POSITIVE_INFINITY);

        this.findStuffFast = findStuffFast;
    }



    public void update(Location phoneLocation,
                       Location targetLocation)
    {
        this.phoneLocation = phoneLocation;
        this.targetLocation = targetLocation;
        redraw();
    }



    @Override
    public void onCompassChanged()
    {
        redraw();
    }



    private void redraw()
    {
        if(!enabled)
            return;

        if(phoneLocation == null || targetLocation == null)
            return;

        float bearing = phoneLocation.bearingTo(targetLocation);

        Compass compass = findStuffFast.getCompass();
        
        Arrow arrow = (Arrow)findStuffFast.findViewById(R.id.arrow1);
        arrow.setDirection(bearing + compass.getLastDirection());
        arrow.invalidate();

        TextView distanceView = (TextView)findStuffFast.findViewById(R.id.distance1);
        distanceView.setText(String.format("(%.1fm)", phoneLocation.distanceTo(targetLocation)));
    }
}